﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConcordWinManager
{
    /// <summary>
    /// Адрес
    /// </summary>
    public class Address
    {
        #region [Constructors]

        public Address()
        {
            id_address = 0;
            regionName = string.Empty;
            regionType = string.Empty;
            nasPunktType = string.Empty;
            nasPunktName = string.Empty;
            streetName = string.Empty;
            streetType = string.Empty;
            homeNumber = string.Empty;
            buildNumber = string.Empty;
            flatNumber = string.Empty;
        }

        public Address(int id)
        {
            SelectById(id);
        }

        public Address(int id_address, string regionName, string regionType,
            string nasPunktName, string nasPunktType, string streetName,
            string streetType, string homeNumber, string buildNumber,
            string flatNumber)
        {
            this.id_address = id_address;
            this.regionName = regionName;
            this.regionType = regionType;
            this.nasPunktType = nasPunktType;
            this.nasPunktName = nasPunktName;
            this.streetName = streetName;
            this.streetType = streetType;
            this.homeNumber = homeNumber;
            this.buildNumber = buildNumber;
            this.flatNumber = flatNumber;
        }

        #endregion

        #region [Fields]
        
        /// <summary>
        /// id адреса в БД
        /// </summary>
        public int id_address;

        /// <summary>
        /// Почтовый индекс
        /// </summary>
        public string index;

        /// <summary>
        /// Название региона
        /// </summary>
        public string regionName;

        /// <summary>
        /// Тип региона
        /// </summary>
        public string regionType;
        //public List<string> regionType = new List<string>(new string[] { "обл.", "край" });

        /// <summary>
        /// Название населенного пункта
        /// </summary>
        public string nasPunktName;

        /// <summary>
        /// Тип населенного пункта
        /// </summary>
        public string nasPunktType;

        /// <summary>
        /// Названи улицы
        /// </summary>
        public string streetName;

        /// <summary>
        /// Тип улицы
        /// </summary>       
        public string streetType;
        
        /// <summary>
        /// Номер дома
        /// </summary>
        public string homeNumber;

        /// <summary>
        /// Номер корпуса
        /// </summary>
        public string buildNumber;

        /// <summary>
        /// Номер квартиры
        /// </summary>
        public string flatNumber;

        #endregion

        #region [Methods]


        /// <summary>
        /// Добавление в бд
        /// </summary>
        public void Insert()
        {
            string query = string.Format("INSERT INTO dbo.Address " +
                " (Address_index, Region_type, Region_name, Nas_punct_name, Nas_punct_type, Street_name, Street_type, Home_number, Build_number, Flat_number) " +
                " VALUES ('{0}','{1}', '{2}', '{3}','{4}', '{5}', '{6}', '{7}', '{8}', '{9}')",
                this.index,
                this.regionType,
                this.regionName,
                this.nasPunktName,
                this.nasPunktType,
                this.streetName,
                this.streetType,
                this.homeNumber,
                this.buildNumber,
                this.flatNumber
                );

            DB.execute_sql(query);
            this.id_address = DB.max_num("dbo.Address", "id_address");
        }

        /// <summary>
        /// Выборка адреса по идентификатору
        /// </summary>
        /// <param name="id">Идентификатор адреса</param>
        /// <returns>Адрес</returns>
        public Address SelectById(int id)
        {
            DataSet ds=new DataSet();
            DB.query_table("SELECT * FROM dbo.Address WHERE id_address = " + id, "dbo.Address", ref ds);

            try
            {
                if (int.TryParse(ds.Tables["dbo.Address"].Rows[0]["id_address"].ToString(), out this.id_address))
                {
                    ;
                }
                //this.id_address = int.Parse(ds.Tables["dbo.Address"].Rows[0]["id_address"].ToString());
                this.index = ds.Tables["dbo.Address"].Rows[0]["Address_index"].ToString();
                this.regionType = ds.Tables["dbo.Address"].Rows[0]["Region_type"].ToString();
                this.regionName = ds.Tables["dbo.Address"].Rows[0]["Region_name"].ToString();
                this.nasPunktName = ds.Tables["dbo.Address"].Rows[0]["Nas_punct_name"].ToString();
                this.nasPunktType = ds.Tables["dbo.Address"].Rows[0]["Nas_punct_type"].ToString();
                this.streetName = ds.Tables["dbo.Address"].Rows[0]["Street_name"].ToString();
                this.streetType = ds.Tables["dbo.Address"].Rows[0]["Street_type"].ToString();
                this.homeNumber = ds.Tables["dbo.Address"].Rows[0]["Home_number"].ToString();
                this.buildNumber = ds.Tables["dbo.Address"].Rows[0]["Build_number"].ToString();
                this.flatNumber = ds.Tables["dbo.Address"].Rows[0]["Flat_number"].ToString();
            }
            catch
            { ;}
            return this;
        }

        public void SelectByArgs()
        {
            ;
        }

        /// <summary>
        /// Обновление записи в БД
        /// </summary>
        public void Update()
        {
            string query = string.Format("UPDATE dbo.Address SET Region_type='{0}', Region_name='{1}', " +
                " Nas_punct_name='{2}', Nas_punct_type='{3}', Street_name='{4}', Street_type='{5}', " +
                " Home_number='{6}', Build_number='{7}', Flat_number='{8}', Address_index='{9}' WHERE (id_address = '{10}')",
                this.regionType,
                this.regionName,
                this.nasPunktName,
                this.nasPunktType,
                this.streetName,
                this.streetType,
                this.homeNumber,
                this.buildNumber,
                this.flatNumber,
                this.index,
                this.id_address);

            DataSet ds=new DataSet();

            DB.query_table(query, "dbo.Address", ref ds);
        }

        public void DeleteById()
        {
            ;
        }

        /// <summary>
        /// Генерация адреса
        /// </summary>
        /// <returns>Адрес в человеческом формате</returns>
        public string RebuildAddressText()
        {
            return string.Format("{9} {0} {1} {2}{3} {4}{5} {6}{7}{8}",
                    this.regionName,
                    this.regionName != "" ? this.regionType + "," : "",
                    this.nasPunktName != "" ? this.nasPunktType + "." : "",
                    this.nasPunktName != "" ? this.nasPunktName + "," : "",
                    this.streetName != "" ? this.streetType + "." : "",
                    this.streetName != "" ? this.streetName + "," : "",
                    this.homeNumber != "" ? "д." + this.homeNumber + "," : "",
                    this.buildNumber != "" ? " корп." + this.buildNumber + "," : "",
                    this.flatNumber != "" ? " кв." + this.flatNumber : "",
                    this.index != "" ? this.index + "," : ""
                    ).Trim();
        }


        #endregion



    }
}
